package internal

import (
	"GameServer/Common/utils"
	"fmt"

	"GameServer/Common/data"
	"GameServer/Game/errors"
	"GameServer/Game/m"
)

func (um *UnionManager) SendEnergy(u *m.User, tuid string, mid string, refreshTime int64) (recv *m.UnionEnergyRecv, send *m.UnionEnergySend, err error) {
	_, ok := m.Cache.Get("unionMembers", u.UnionId)
	if !ok {
		err = errors.NewErrcode(data.Errcode.NoUnion, fmt.Sprintf("公会成员获取失败,id:%s", u.UnionId))
		return
	}
	// 被赠送体力的玩家，不在公会，任然能赠送体力消息，只是不给玩家。
	// mMembers := v.(*m.UnionMembers)
	// _, ok = mMembers.GetMember(tuid)
	// if !ok {
	// 	err = errors.NewErrcode(data.Errcode.NoUnionUser, fmt.Sprintf("公会id%s, 没有该成员,uid:%s", u.UnionId, tuid))
	// 	return
	// }
	uEnergy := u.GetUnionEnergy()
	if uEnergy.IsSend(tuid, mid) {
		err = errors.NewErrcode(data.Errcode.ServerErr, fmt.Sprintf("已经赠送过,tuid:%s", tuid))
		return
	}
	recv = &m.UnionEnergyRecv{
		Uid:       u.Uid.Hex(),
		Name:      u.Name,
		Head:      u.Head,
		EnergyNum: 1,
		Time:      utils.TNow().Unix(),
	}
	send = &m.UnionEnergySend{Uid: tuid, RefreshTime: refreshTime, Mid: mid}
	uEnergy.AddSends(send)
	return
}
